package com.cefa.edu.controller;

import com.cefa.edu.common.result.FailureResult;
import com.cefa.edu.common.result.Result;
import com.cefa.edu.common.result.ReturnMessage;
import com.cefa.edu.common.result.SuccessResult;
import com.cefa.edu.dao.UserOrganRoleMapper;
import com.cefa.edu.model.CefaEduSample;
import com.cefa.edu.model.User;
import com.cefa.edu.service.LoginService;
import com.cefa.edu.service.SampleService;
import com.cefa.edu.service.ScoreService;
import com.cefa.edu.service.UserService;
import com.cefa.edu.util.MD5Generator;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author huangzehui
 * @date 18-7-29 上午10:49
 */

@RestController
public class loginController {
    private Logger logger = LoggerFactory.getLogger(loginController.class);

    @Autowired
    LoginService loginService;
    @Autowired
    ScoreService scoreService;
    @Autowired
    UserService userService;

    @Autowired
    UserOrganRoleMapper userOrganRoleMapper;

    /**
     * 登录
     * @param password
     * @param name
     * @return
     */
    @RequestMapping(path="/education/login", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Map<String,Object>> login(@RequestParam(name = "password") String password, @RequestParam(name = "userName") String name) {
        List<com.cefa.edu.pojo.User> userList = loginService.getUser(MD5Generator.generatorMD5(password) ,name);
        Integer count=0;
        boolean falt=false;
        Map<String, Object> result = Maps.newHashMap();
        if(userList !=null&& !userList.isEmpty()){
            com.cefa.edu.pojo.User user=userList.get(0);
            String schoolName=userOrganRoleMapper.findSchoolName(user.getId());
            count= scoreService.studentChainDataCount(user.getId(),user.getRoleType(),user.getSchoolId());
            falt=true;
            result.put("falt",falt);
            result.put("message","密码正确！");
            result.put("user", user);
            result.put("count",count);
            result.put("schoolName",schoolName);
        }else{
            result.put("falt",falt);
            result.put("message","密码错误！");
            result.put("user", null);
        }
        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
    }


    /**
     * 验证用户是否存在
     * @param name
     * @return
     */
    @RequestMapping(path="/education/userValidate", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Map<String,Object>> userValidate(@RequestParam(name = "userName") String name) {
        int count = loginService.getUserByUserName(name);
        boolean falt=false;
        Map<String, Object> result = Maps.newHashMap();
        if(count>0){
            falt=true;
            result.put("falt",falt);
            result.put("message","用户存在！");
        }else{
            result.put("falt",falt);
            result.put("message","用户不存在！");
        }
        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
    }

    @RequestMapping(path="/education/getUserKey", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Map<String,Object>> getUserKey(Integer userId) {
        com.cefa.edu.pojo.User user=userService.getUserById(userId);
        boolean falt=false;
        Map<String, Object> result = Maps.newHashMap();
        if(user!=null){
            falt=true;
            result.put("falt",falt);
        }else{
            result.put("falt",falt);
        }
        result.put("key",user.getKeyData());
        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
    }

    @RequestMapping(path="/education/getCount", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Map<String,Object>> getCount(Integer userId,Integer roleType,Integer schoolId) {
        boolean falt=false;
        Map<String, Object> result = Maps.newHashMap();
        int count=0;
        try{
            count= scoreService.studentChainDataCount(userId,roleType,schoolId);
            falt=true;
            result.put("falt",falt);
            result.put("count",count);
        }catch(Exception e){
            logger.error(e.getMessage());
            result.put("falt",falt);
            result.put("count",count);
            return new FailureResult<>(new ReturnMessage("500", e.getMessage()), result);
        }

        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
    }
}
